package com.pdstudio.charts;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.pdstudio.apapter.WaterStationAdapter;
import com.pdstudio.app.Application;
import com.pdstudio.app.Application.EventHandler;
import com.pdstudio.bean.StationState;
import com.pdstudio.bean.WaterStation;
import com.pdstudio.http.StationHttpMethod;
import com.pdstudio.util.SharePreferenceUtil;
import com.pdstudio.view.SwipeBackActivity;
import com.pdstudio.weather.LiveIndexActivity;
import com.pdstudio.weather.R;

public class StationListStateActivity extends Activity implements EventHandler {

	private View customStationListTitleView;
	private TextView txtTitle;
	private ImageView backImageButton;
	private ImageView ivBack;
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;

	private StationListAdapter stationListAdapter;
	private ListView lvStation;
	private Handler handler = null;

	private int selectIndex = 0;// 0表示全部，1表示第一个，2表示第二个类推
	private TextView allButton;
	private TextView oneButton;
	private TextView twoButton;
	private TextView threeButton;
	private TextView fourButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_station_list_main);

		Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();

		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_stationlist);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));

		customStationListTitleView = findViewById(R.id.title_stationlist);
		txtTitle = (TextView) customStationListTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("站点列表");

		ivBack = (ImageView) findViewById(R.id.title2_back);
		ivBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				StationListStateActivity.this.finish();
			}

		});

		allButton = (TextView) findViewById(R.id.station_all_list);
		allButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelectIndex(0);
			}
		});
		oneButton = (TextView) findViewById(R.id.station_one_list);
		oneButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelectIndex(1);
			}
		});

		twoButton = (TextView) findViewById(R.id.station_two_list);
		twoButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelectIndex(2);
			}
		});

		threeButton = (TextView) findViewById(R.id.station_three_list);
		threeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelectIndex(3);
			}
		});

		fourButton = (TextView) findViewById(R.id.station_four_list);
		fourButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setSelectIndex(4);
			}
		});

		setSelectIndex(0);

		lvStation = (ListView) findViewById(R.id.stationstatelist);
		// android.widget.LinearLayout.LayoutParams layoutParams=new
		// android.widget.LinearLayout.LayoutParams(lvStation.getWidth(),lvStation.getHeight()-300);
		// lvStation.setLayoutParams(layoutParams);

		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 0) {
					List<StationState> wds = null;
					try {
					if (msg.obj != null) {
						
							wds = (List<StationState>) msg.obj;
							if(wds.size()<1){
								StationState s=new StationState();
								s.address="没有符合条件的记录";
								wds.add(s);
							}
							SetStationStatesAdapter(wds);
					}else{
						wds=new ArrayList<StationState>();
						StationState s=new StationState();
						s.address="没有符合条件的记录";
						wds.add(s);
						SetStationStatesAdapter(wds);
					}} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		};

	}

	public void setSelectIndex(int flag) {
		try {
			selectIndex=flag;
			switch (flag) {
			case 0: {
				allButton.setBackgroundColor(0xbf0d4d76);
				oneButton.setBackgroundColor(0x00000000);
				twoButton.setBackgroundColor(0x00000000);
				threeButton.setBackgroundColor(0x00000000);
				fourButton.setBackgroundColor(0x00000000);
			}
				break;
			case 1: {
				allButton.setBackgroundColor(0x00000000);
				oneButton.setBackgroundColor(0xbf0d4d76);
				twoButton.setBackgroundColor(0x00000000);
				threeButton.setBackgroundColor(0x00000000);
				fourButton.setBackgroundColor(0x00000000);
			}
				break;
			case 2: {
				allButton.setBackgroundColor(0x00000000);
				oneButton.setBackgroundColor(0x00000000);
				twoButton.setBackgroundColor(0xbf0d4d76);
				threeButton.setBackgroundColor(0x00000000);
				fourButton.setBackgroundColor(0x00000000);
			}
				break;
			case 3: {
				allButton.setBackgroundColor(0x00000000);
				oneButton.setBackgroundColor(0x00000000);
				twoButton.setBackgroundColor(0x00000000);
				threeButton.setBackgroundColor(0xbf0d4d76);
				fourButton.setBackgroundColor(0x00000000);
			}
				break;
			case 4: {
				allButton.setBackgroundColor(0x00000000);
				oneButton.setBackgroundColor(0x00000000);
				twoButton.setBackgroundColor(0x00000000);
				threeButton.setBackgroundColor(0x00000000);
				fourButton.setBackgroundColor(0xbf0d4d76);
			}
				break;
			default: {
				allButton.setBackgroundColor(0x00000000);
				oneButton.setBackgroundColor(0x00000000);
				twoButton.setBackgroundColor(0x00000000);
				threeButton.setBackgroundColor(0x00000000);
				fourButton.setBackgroundColor(0x00000000);
			}
				break;
			}
			
			Thread thread = new Thread() {
				@Override
				public void run() {
					try {
						StationHttpMethod shm = new StationHttpMethod(
								StationListStateActivity.this);
						List<StationState> si = shm.GetStationStatesList(Application
								.getInstance().getSharePreferenceUtil()
								.getAuthCode(), Application.getInstance()
								.getCurSimpleCity().cityCode, Application
								.getInstance().getCurSimpleCity().cityCode,
								"2014-11-3-4",selectIndex);
						if (si != null) {
							Message m = handler.obtainMessage(0, si);
							handler.sendMessage(m);
						}
					} catch (Exception e) {
						e.printStackTrace();
						Toast.makeText(getApplicationContext(), "网络异常，稍后重试！", 0)
								.show();
					}
				}
			};
			thread.start();
		} catch (Exception e) {
		}
	}

	private void SetStationStatesAdapter(List<StationState> si) {
		try {
			stationListAdapter = new StationListAdapter(si, this,
					R.layout.activity_station_list_main_item);
			lvStation.setAdapter(stationListAdapter);
		} catch (Exception e) {
		}
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub

	}

	public class StationListAdapter extends BaseAdapter {

		private List<StationState> list;
		private Context context;
		private int layout;

		private LayoutInflater inflater;

		public StationListAdapter(List<StationState> list, Context context,
				int layout) {
			this.list = list;
			this.context = context;
			this.layout = layout;

			inflater = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		}

		/*
		 * 获取总行数
		 * 
		 * @see android.widget.Adapter#getCount()
		 */
		public int getCount() {
			return list.size();
		}

		/*
		 * 根据索引获取数据
		 * 
		 * @see android.widget.Adapter#getItem(int)
		 */
		public Object getItem(int position) {
			return list.get(position);
		}

		/*
		 * item的id，很少使用到
		 * 
		 * @see android.widget.Adapter#getItemId(int)
		 */
		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewStation cache;
			if (convertView == null) {
				convertView = inflater.inflate(layout, null);
				cache = new ViewStation();
				cache.stationname = (TextView) convertView
						.findViewById(R.id.station_list_stationname);
				cache.rain = (TextView) convertView
						.findViewById(R.id.station_list_droprain);
				cache.temperature = (TextView) convertView
						.findViewById(R.id.station_list_temperature);
				cache.flow = (TextView) convertView
						.findViewById(R.id.station_list_flow);
				cache.speed = (TextView) convertView
						.findViewById(R.id.station_list_speed);
				convertView.setTag(cache);
			} else {
				cache = (ViewStation) convertView.getTag();
			}

			StationState t = list.get(position);
			cache.stationname.setText(t.address);
			cache.rain.setText(t.precipitation);
			cache.temperature.setText(t.temperature);
			cache.flow.setText(t.wind);
			cache.speed.setText(t.windDirection);

			return convertView;
		}

		public final class ViewStation {
			public TextView stationname;
			public TextView rain;
			public TextView temperature;
			public TextView flow;
			public TextView speed;
		}

	}
}
